package pl.szlif.AChecker.AUtils;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;

public class AccelerometerManager {
	private static final String TAG = "Shake";
	private static Sensor sensor;
	private static SensorManager sensorManager;
	private static AccelerometrListener listener;
	private static boolean supported;
	private static boolean running;
	private static Activity a;

	private static float threshold = 0.0000005f;
	private static int interval = 700000000;
		
	public static void setActivity(Activity newActivity){
		a = newActivity;
	}

	public static boolean isListening() {
		return running;
	}

	public static void stopListening() {
		running = false;
		try {
			if (sensorManager != null && sensorEventListener != null) {
				sensorManager.unregisterListener(sensorEventListener);
			}
		} catch (Exception e) {
		}
	}

	public static boolean isSupported() {
		if (supported == false) {
			if (a != null) {
				sensorManager = (SensorManager) a.getSystemService(Context.SENSOR_SERVICE);
				List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
				supported = new Boolean(sensors.size() > 0);
			} else {
				supported = Boolean.FALSE;
			}
		}
		return supported;
	}

	public static void startListening(AccelerometrListener accelerometerListener) {
		sensorManager = (SensorManager) a.getSystemService(Context.SENSOR_SERVICE);
		List<Sensor> sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
		if (sensors.size() > 0) {
			sensor = sensors.get(0);
			running = sensorManager.registerListener(sensorEventListener, sensor, SensorManager.SENSOR_DELAY_GAME);
			listener = accelerometerListener;
		}
	}

	private static SensorEventListener sensorEventListener = new SensorEventListener() {

		private long now = 0;
		private long timeDiff = 0;
		private long lastUpdate = 0;
		private long lastShake = 0;

		private float x = 0;
		private float y = 0;
		private float z = 0;
		private float lastX = 0;
		private float lastY = 0;
		private float lastZ = 0;
		private float force = 0;

		public void onAccuracyChanged(Sensor sensor, int accuracy) {
		}

		public void onSensorChanged(SensorEvent event) {
			now = event.timestamp;
			// time = System.currentTimeMillis();

			x = event.values[0];
			y = event.values[1];
			z = event.values[2];

			if (lastUpdate == 0) {
				//Log.d(TAG, "lastUpdate == 0 true");
				lastUpdate = now;
				lastShake = now;
				lastX = x;
				lastY = y;
				lastZ = z;
			} else {
				//Log.d(TAG, "lastUpdate == 0 false");
				timeDiff = now - lastUpdate;
				if (timeDiff > 0) {
					force = Math.abs(x + y + z - lastX - lastY - lastZ) / timeDiff;
					// Log.d(TAG, "force: "+ force + "threadshold" + threshold);
					if (force > threshold) {
						// Log.d("shake","now: " + now + "lastShake: " +
						// lastShake + " interval: " +interval + "diff: " + (now
						// - lastShake));
						if (now - lastShake >= interval) {
							
							float vectorX = lastX - x;
							float vectorY = lastY - y;
							float vectorZ = lastZ - z;
							String shake = "Move vector x:" + vectorX+ " y:" + vectorY + " z: " +vectorZ;
							Log.d(TAG, shake );
							listener.onShake(shake);
							lastShake = now;
						}

					}
					lastX = x;
					lastY = y;
					lastZ = z;
					lastUpdate = now;
				}
			}
		}
	};
}

